﻿using System;
using System.Diagnostics;

namespace Results.Models
{
    /// <summary>
    /// Holds all the data
    /// </summary>
    public class MainModel
    {
        public ResultsModel ResultsModel { get; private set; }
        public TournamentsModel TournamentsModel { get; private set; }
        public PlayersModel PlayersModel { get; private set; }

        /// <summary>
        /// Signal when all the data is loaded
        /// </summary>
        public event EventHandler<MainModelLoadedEventArgs> MainModelLoaded;

        public MainModel()
        {
            ResultsModel = new ResultsModel();
            ResultsModel.ResultsModelLoaded += OnResultsModelLoaded;

            TournamentsModel = new TournamentsModel();
            TournamentsModel.TournamentsModelLoaded += OnTournamentsModelLoaded;

            PlayersModel = new PlayersModel();

            _path = @"http://www.premierjuniortour.com/results/";

            SetDebugPath();
        }

        [Conditional("DEBUG")]
        void SetDebugPath()
        {
            _path = @"http://localhost:1206/";
        }

        /// <summary>
        /// Load the data.  Starts a chain of events.
        /// </summary>
        public void Load()
        {
            // start by loading the results
            ResultsModel.Load(_path);
        }

        /// <summary>
        /// Callback when the results are loaded
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void OnResultsModelLoaded(object sender, ResultsModelLoadedEventArgs e)
        {
            if (e.Loaded)
            {
                // continue by loading the tournaments
                TournamentsModel.Load(_path);
            }
            else
            {
                _status = e.Status;
                Notify();
            }
        }

        /// <summary>
        /// Callback when the tournaments are loaded
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void OnTournamentsModelLoaded(object sender, TournamentsModelLoadedEventArgs e)
        {
            if (e.Loaded)
            {
                // conclude by building a list of players from the results, and assining results to tournaments
                PlayersModel.Load(ResultsModel);
                TournamentsModel.AssignResults(ResultsModel);
            }
            else
            {
                _status = e.Status;
            }

            _loaded = e.Loaded;

            Notify();
        }

        /// <summary>
        /// Notification that the data was loaded
        /// </summary>
        void Notify()
        {
            var mainModelLoaded = MainModelLoaded;

            if (mainModelLoaded != null)
            {
                mainModelLoaded.Invoke(this, new MainModelLoadedEventArgs { Loaded = _loaded, Status = _status });
            }
        }

        String _path;
        Boolean _loaded;
        String _status;
    }

    public class MainModelLoadedEventArgs : EventArgs
    {
        public Boolean Loaded { get; set; }
        public String Status { get; set; }
    }
}
